# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
# from jms.dm.dm_waybill_false_taking_dt import jms_dm__dm_waybill_false_taking_dt

label = f"dm_waybill_false_taking_dt_{int(time())}"
timeout = timedelta(hours=1).seconds
doris_jms_dm__dm_waybill_false_taking_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_waybill_false_taking_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='new_doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_waybill_false_taking_dt PARTITION (
         p{{{{ execution_date | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-1) |cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-2) |cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-3) |cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-4) |cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-5) |cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-6) |cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_false_taking_dt/dt={{{{ execution_date | cst_ds }}}}/*",
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_false_taking_dt/dt={{{{ execution_date | date_add(-1) |cst_ds }}}}/*",
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_false_taking_dt/dt={{{{ execution_date | date_add(-2) |cst_ds }}}}/*",
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_false_taking_dt/dt={{{{ execution_date | date_add(-3) |cst_ds }}}}/*",
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_false_taking_dt/dt={{{{ execution_date | date_add(-4) |cst_ds }}}}/*",
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_false_taking_dt/dt={{{{ execution_date | date_add(-5) |cst_ds }}}}/*",
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_false_taking_dt/dt={{{{ execution_date | date_add(-6) |cst_ds }}}}/*"
        )   
        INTO TABLE dm_waybill_false_taking_dt
        FORMAT AS 'PARQUET'(
            taking_date
           ,waybill_no
           ,receive_network_code
           ,receive_fran_code
           ,receive_agent_code
           ,is_break
           ,break_time
           ,is_span_agent
           ,is_entrepot_intime
           ,is_center_intime
           ,is_intime
           ,is_over
           ,is_handover_intime
           ,handover_site_code
           ,handover_site_name
           ,goods_type_code
           ,goods_type_name
           ,customer_code
           ,order_source_code
           ,type
           ,total_days_t
           ,receive_network_name
           ,receive_fran_name
           ,receive_agent_name
           ,receive_manage_code
           ,receive_manage_name
           ,receive_city_code
           ,receive_city_name
           ,first_taking_scan_time
           ,first_network_send_time
           ,first_entrepot_actual_code
           ,first_entrepot_actual_name
           ,first_entrepot_actual_time
           ,first_entrepot_plan_code
           ,first_entrepot_plan_name
           ,first_entrepot_plan_time
           ,first_actual_center_code
           ,first_center_actual_name
           ,first_center_actual_time
           ,first_center_shipment_no
           ,first_center_plan_time
           ,first_center_plan_date
           ,first_center_last_plan_time
           ,first_center_departure_time
           ,first_center_agent_code
           ,first_center_agent_name
           ,last_network_code
           ,last_network_name
           ,customer_name           
           ,if_center_arrival       
        )
        columns from path as (dt)
        set(
            taking_date                   = taking_date
           ,waybill_no                    = waybill_no
           ,receive_network_code          = receive_network_code
           ,receive_fran_code             = receive_fran_code
           ,receive_agent_code            = receive_agent_code
           ,is_break                      = is_break
           ,break_time                    = break_time
           ,is_span_agent                 = is_span_agent
           ,is_entrepot_intime            = is_entrepot_intime
           ,is_center_intime              = is_center_intime
           ,is_intime                     = is_intime
           ,is_over                       = is_over
           ,is_handover_intime            = is_handover_intime
           ,handover_site_code            = handover_site_code
           ,handover_site_name            = handover_site_name
           ,goods_type_code               = goods_type_code
           ,goods_type_name               = goods_type_name
           ,customer_code                 = customer_code
           ,order_source_code             = order_source_code
           ,type                          = type
           ,total_days_t                  = total_days_t
           ,receive_network_name          = receive_network_name
           ,receive_fran_name             = receive_fran_name
           ,receive_agent_name            = receive_agent_name
           ,receive_manage_code           = receive_manage_code
           ,receive_manage_name           = receive_manage_name
           ,receive_city_code             = receive_city_code
           ,receive_city_name             = receive_city_name
           ,first_taking_scan_time        = first_taking_scan_time
           ,first_network_send_time       = first_network_send_time
           ,first_entrepot_actual_code    = first_entrepot_actual_code
           ,first_entrepot_actual_name    = first_entrepot_actual_name
           ,first_entrepot_actual_time    = first_entrepot_actual_time
           ,first_entrepot_plan_code      = first_entrepot_plan_code
           ,first_entrepot_plan_name      = first_entrepot_plan_name
           ,first_entrepot_plan_time      = first_entrepot_plan_time
           ,first_actual_center_code      = first_actual_center_code
           ,first_center_actual_name      = first_center_actual_name
           ,first_center_actual_time      = first_center_actual_time
           ,first_center_shipment_no      = first_center_shipment_no
           ,first_center_plan_time        = first_center_plan_time
           ,first_center_plan_date        = first_center_plan_date
           ,first_center_last_plan_time   = first_center_last_plan_time
           ,first_center_departure_time   = first_center_departure_time
           ,first_center_agent_code       = first_center_agent_code
           ,first_center_agent_name       = first_center_agent_name
           ,last_network_code             = last_network_code
           ,last_network_name             = last_network_name
           ,customer_name                 = customer_name     
           ,if_center_arrival             = if_center_arrival 
           ,dt                            = dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dm__dm_waybill_false_taking_dt << jms_dm__dm_waybill_false_taking_dt
